/**
 * Created by zzy on 2017/1/13.
 */
var $ = require('jquery');
var $alert = require('libs/alert');
var $confirm = require('libs/confirm');
require('easyui');
var issys  = {"0":"否","1":"是"};
var isSale = require('libs/user-type').isSale;
var clickEvent = {
    modifyInit:function () {
        $("body").on("click","a[name=modify]",function () {
            var id = $(this).parents("tr").children(":first").text();
            window.location.href="authority-update?id="+id;
        });
        return clickEvent;
    },
    deleteInit:function () {
        $("body").on("click","a[name=delete]",function () {
            var id = $(this).parents("tr").children(":first").text();
            $confirm("确认删除该角色吗?",function () {
                $.ajax({
                    url:"api/v1/p/mms/authAuthority/delete",
                    type:"post",
                    dataType:"json",
                    contentType: "application/json; charset=utf-8",
                    data:JSON.stringify({
                        "id":id
                    }),
                    success:function (result) {
                        if(result.code=="200") {
                            $alert("删除成功!");
                            $("#main_table").datagrid("reload");
                        }else{
                            $alert(result.message);
                        }
                    }
                })
            })
        });
        return clickEvent;
    },
    searchInit:function () {
        $("#a_search").on("click",function () {
            $("#main_table").datagrid('load', {
                systemId: $('#select_system').combobox('getValue')
            })
        });
        return clickEvent;
    },
    refreshResourceAuthoritisCommit:function () {
        $('#over_pallet').on('click', function () {
            if (checkInput($('#select_systemName').combobox('getValue'))) {
                var data = {};
                data["password"] = $('#password').val();
                data["systemName"] = $('#select_systemName').combobox('getValue');
                $.ajax({
                    url:"api/v1/p/mms/user/refreshResourceAuthoritis",
                    type:'post',
                    contentType: "application/json; charset=utf-8",
                    data:JSON.stringify(data),
                    dataType:'json',
                    success:function (result) {
                        if(result.code=="200") {
                            $alert("刷新成功!");
                            $("#pack_weight_modal").modal('hide');
                        }else{
                            $alert(result.message);
                        }
                    }
                })
            }
        });
        return clickEvent;
    },
    refreshResourceAuthoritis:function () {
        $("#a_refresh").on("click",function () {
            $('#password').val('');
            $(".test-success").remove();
            $(".test-error").remove();
            $("#pack_weight_modal").modal('show');

        });
        return clickEvent;
    },
    detailInit:function () {
        $("body").on("click","a[name=authorityName]",function () {
            var id = $(this).parents("tr").children(":first").text();
            window.location.href="authority-detail?id="+id;
        });
        return clickEvent;
    }
};
$(function(){
    pageInit();
    mainTableInit();
});
function pageInit(){
    $("#a_create").show();
    clickEvent.modifyInit().deleteInit().searchInit().detailInit().refreshResourceAuthoritis().refreshResourceAuthoritisCommit();
}
//校验check
function checkInput(systemName) {
    var flag = true;
    if(systemName != 'MMS'){
        $("#pack_form input").each(function () {
            var $div = $(this).parent().next("div");
            var span =" <span class='test-success'><i class='glyphicon glyphicon-ok'></i></span>";
            if(this.value==''){
                var span ="<span class='test-error'><i class='glyphicon glyphicon-remove l-8'></i>请输入密码</span>"
                $div.empty();$div.append(span);
                flag = false;
            }
        });

    }
    return flag;
}
function mainTableInit(){
    $("#main_table").datagrid({
        url:'api/v1/p/mms/authAuthority/list',
        border : false,
        height:$(window).height(),
        pagination : true,
        idField : 'id',
        pageSize : 20,
        pageList : [ 20, 50,  100 ],
        checkOnSelect : true,//单击选中
        emptyMsg : "暂无数据",
        selectOnCheck : true,
        singleSelect : true,
        rownumbers:true,
        columns:[[
            {
                field : 'id',
                hidden:true
            },{
                field:'authorityName',
                title:'权限名称',
                width:200,
                formatter:function (value,row,index) {
                    return "<a href='javascript:;' name='authorityName'>"+value+"</a>"
                }
            },{
                field:'authorityMark',
                title:'权限标示',
                width:200
            },
            {
                field:'issys',
                title:'是否系统级别',
                width:200,
                formatter:function (value,row,index) {
                    return issys[value];
                }
            },{
                field:'systemName',
                title:'所属系统',
                width:200
            },{
                field:'opertation',
                title:'操作',
                hidden:isSale,
                width:300,
                formatter:function (value,row,index) {
                    if(row.enable=="0"){
                        return "<a href='javascript:;' name='modify'>修改</a>|<a href='javascript:;' name='delete'>删除</a>";
                    }else{
                        return "<a href='javascript:;' name='modify'>修改</a>|<a href='javascript:;' name='delete'>删除</a>";
                    }
                }
            }
        ]],
        loadFilter:function(data){
            if(data.code!='200'){
                return data.data;
            }
            return data.data;
        },
        onLoadSuccess:function(data){

        },
        toolbar:"#tb"
    })
}